package com.vanhon.engine.decrypt;

import android.util.Log;
import java.io.IOException;
import java.io.InputStream;
import java.util.Locale;

/* loaded from: classes.dex */
public class DecryptStream extends InputStream {
    private static final int HEAD_LEN = 1024;
    private static final String TAG = "DecryptStream";
    private Decrypt mDecoder;
    private int mHeadSize;
    private DecryptSource mInputSource;
    private int mLength;
    private int mPos;

    public static DecryptStream create(String str) {
        DecryptSource decryptFileSource;
        if (str == null) {
            return null;
        }
        String lowerCase = str.toLowerCase(Locale.CHINA);
        if (lowerCase.startsWith("http://")) {
            decryptFileSource = new DecryptHttpSource();
            if (!decryptFileSource.open(str)) {
                return null;
            }
        } else {
            if (lowerCase.startsWith("file://")) {
                decryptFileSource = new DecryptFileSource();
                if (!decryptFileSource.open(str.substring(7))) {
                    return null;
                }
            }
            return null;
        }
        try {
            return new DecryptStream(decryptFileSource);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public DecryptStream(DecryptSource decryptSource) throws IOException {
        this.mPos = 0;
        this.mLength = 0;
        this.mHeadSize = 0;
        this.mInputSource = decryptSource;
        this.mLength = (int) this.mInputSource.length();
        byte[] bArr = new byte[1024];
        byte[] bArr2 = new byte[1024];
        try {
            int read = this.mInputSource.read(bArr);
            if (read >= 0) {
                while (read < bArr.length) {
                    bArr[read] = 0;
                    read++;
                }
            }
            this.mDecoder = new Decrypt(bArr);
            this.mHeadSize = (int) this.mDecoder.decrypt(bArr, 0L, bArr2);
            int length = bArr2.length - this.mHeadSize;
            this.mHeadSize = length;
            this.mPos = length;
            Log.e(TAG, "DecryptStream================mHeadSizeEx: " + this.mHeadSize);
        } catch (Exception e) {
            this.mInputSource = null;
            e.printStackTrace();
        }
    }

    public int getLength() {
        int i = this.mLength;
        if (i == -1) {
            return -1;
        }
        return i - this.mHeadSize;
    }

    public void setLength(int i) {
        this.mLength = this.mHeadSize + i;
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        byte[] bArr = new byte[1];
        if (read(bArr) < 1) {
            return -1;
        }
        return bArr[0] & 255;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr) throws IOException {
        return read(bArr, 0, bArr.length);
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        if (i2 < 0 || i < 0 || i + i2 > bArr.length) {
            Log.e(TAG, "read [], invalid arguments. offset: " + i + ", len: " + i2 + ", length: " + bArr.length);
            return 0;
        }
        this.mInputSource.seek(this.mPos);
        byte[] bArr2 = new byte[i2];
        int read = this.mInputSource.read(bArr2, 0, bArr2.length);
        if (read != -1) {
            int decrypt = (int) this.mDecoder.decrypt(this.mPos, bArr2, 0, read, bArr, i, i2);
            this.mPos += read;
            return decrypt;
        }
        return -1;
    }

    @Override // java.io.InputStream
    public long skip(long j) throws IOException {
        if (j <= 0) {
            return 0L;
        }
        this.mPos = (int) (this.mPos + j);
        int i = this.mPos;
        int i2 = this.mLength;
        if (i > i2) {
            this.mPos = i2;
        }
        return j;
    }

    @Override // java.io.InputStream
    public int available() throws IOException {
        int i = this.mLength;
        if (i != -1) {
            return i - this.mPos;
        }
        return super.available();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        super.close();
        DecryptSource decryptSource = this.mInputSource;
        if (decryptSource != null) {
            decryptSource.close();
        }
    }
}
